package com.bwhx.eruler.controller.casdilly;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.bean.casdilly.BodyItem;
import com.bwhx.eruler.controller.BaseController;
import com.bwhx.eruler.service.casdilly.BodyItemService;
@Controller
public class BodyItemController extends BaseController {
	
	@Autowired
	BodyItemService bodyItemService;
	
	
	@ResponseBody
	@RequestMapping(value = "/page.saveCeliangbuwei.ashx")
	public String saveOOrder(BodyItem bodyItem,HttpSession session) {
		try{
			Staff staff = (Staff) session.getAttribute("staff");
			bodyItem.setCreateTime(new Date());
			bodyItem.setCreatorID(staff.getsId().toString());
			
			BodyItem findByName = bodyItemService.findByName(bodyItem.getItemName());
			if(null!=findByName){
				return "conflict";
			}
			bodyItemService.insert(bodyItem);
		
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.celiangbuweiList.ashx")
	 public Map<String,Object> orderList(Integer page,Integer rows,BodyItem bodyItem) {
		
		
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,bodyItem);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			List<BodyItem> list = bodyItemService.list(p);
			Integer count = bodyItemService.count(bodyItem);

					
			resultMap.put("total",count );//total键 存放总记录数，必须的  
			resultMap.put("rows",list);//rows键 存放每页记录 list 
			
		
			
		}catch(Exception e){
			log.error(getTrace(e));
			resultMap.put("msg", "失败！");
			resultMap.put("status", "0");
		}
		
	    return resultMap;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.celiangbuweiListAll.ashx")
	 public List<BodyItem> bodyItemListAll() {
		
		BodyItem celiangBuwei = new BodyItem();
        Page p = new Page(0,0,celiangBuwei);
		List<BodyItem> list = bodyItemService.list(p);
	    return list;  
	  }

	@ResponseBody
	@RequestMapping(value = "/page.celiangbuweidel.ashx")
	 public Map<String,String> OfflineOrderDel(BodyItem bodyItem){
		Map<String,String> resultMap = new HashMap<String, String>();
		Staff staff = this.getSessionStaff();
		bodyItem.setModifierID(staff.getsId().toString());
		bodyItem.setModifyTime(new Date());
		
		bodyItemService.del(bodyItem);
		resultMap.put("status", "1");
	    return resultMap;  
	  }
	
	
	@ResponseBody
	@RequestMapping(value = "/page.everyone.celiangbuweiListByIds.ashx")
	 public List<BodyItem> bodyItemListByIds(String ids) {
		
		String[] idsArr = ids.split(",");
		
		List<BodyItem> list = bodyItemService.listByIds(idsArr);
	    return list;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.everyone.otherCeliangbuweiByIds.ashx")
	 public List<BodyItem> otherCeliangbuweiByIds(String ids) {
		
		String[] idsArr = ids.split(",");
		
		List<BodyItem> list = bodyItemService.notInIds(idsArr);
	    return list;  
	  }

}
